package org.javaweb.cms_web.mapper;

import org.apache.ibatis.annotations.*;
import org.javaweb.cms_web.model.Feedback;

import java.util.List;

@Mapper
public interface FeedbackMapper {

    // 获取所有反馈列表，联表查询获取用户昵称，支持筛选和搜索
    @Select("<script>" +
            "SELECT f.feedback_id, f.title, f.description, f.user_id, f.submit_date, f.status, u.nickname " +
            "FROM Feedbacks f " +
            "JOIN Users u ON f.user_id = u.user_id " +
            "<where>" +
            "   <if test='searchKeyword != null and searchKeyword != \"\"'> AND f.title LIKE CONCAT('%', #{searchKeyword}, '%') </if>" +
            "   <if test='statusFilter != null and statusFilter != \"\"'> AND f.status = #{statusFilter} </if>" +
            "   <if test='timeFilter != null and timeFilter != \"\"'>" +
            "       <choose>" +
            "           <when test='timeFilter == \"1\"'> AND f.submit_date >= NOW() - INTERVAL 1 DAY </when>" +
            "           <when test='timeFilter == \"7\"'> AND f.submit_date >= NOW() - INTERVAL 7 DAY </when>" +
            "           <when test='timeFilter == \"30\"'> AND f.submit_date >= NOW() - INTERVAL 30 DAY </when>" +
            "       </choose>" +
            "   </if>" +
            "</where>" +
            "</script>")
    @Results({
            @Result(property = "feedbackId", column = "feedback_id"),
            @Result(property = "title", column = "title"),
            @Result(property = "description", column = "description"),
            @Result(property = "userId", column = "user_id"),
            @Result(property = "submitDate", column = "submit_date"),
            @Result(property = "status", column = "status"),
            @Result(property = "nickname", column = "nickname") // 映射用户的昵称
    })
    List<Feedback> findFeedbacksWithFilters(@Param("searchKeyword") String searchKeyword,
                                            @Param("statusFilter") String statusFilter,
                                            @Param("timeFilter") String timeFilter);


    // 获取所有反馈列表，联表查询获取用户昵称
    @Select("SELECT f.feedback_id, f.title, f.description, f.user_id, f.submit_date, f.status, u.nickname " +
            "FROM Feedbacks f " +
            "JOIN Users u ON f.user_id = u.user_id")
    @Results({
            @Result(property = "feedbackId", column = "feedback_id"),
            @Result(property = "title", column = "title"),
            @Result(property = "description", column = "description"),
            @Result(property = "userId", column = "user_id"),
            @Result(property = "submitDate", column = "submit_date"),
            @Result(property = "status", column = "status"),
            @Result(property = "nickname", column = "nickname") // 映射用户的昵称
    })
    List<Feedback> findAllFeedbacks();

    // 根据ID查询反馈详情，联表查询获取用户昵称
    @Select("SELECT f.feedback_id, f.title, f.description, f.user_id, f.submit_date, f.status, u.nickname " +
            "FROM Feedbacks f " +
            "JOIN Users u ON f.user_id = u.user_id " +
            "WHERE f.feedback_id = #{feedbackId}")
    @Results({
            @Result(property = "feedbackId", column = "feedback_id"),
            @Result(property = "title", column = "title"),
            @Result(property = "description", column = "description"),
            @Result(property = "userId", column = "user_id"),
            @Result(property = "submitDate", column = "submit_date"),
            @Result(property = "status", column = "status"),
            @Result(property = "nickname", column = "nickname") // 映射用户的昵称
    })
    Feedback findById(@Param("feedbackId") Integer feedbackId);

    // 更新反馈的状态
    @Update("UPDATE Feedbacks SET status = #{status}, reply_content = #{replyContent} WHERE feedback_id = #{feedbackId}")
    int updateFeedbackStatus(@Param("feedbackId") Integer feedbackId, @Param("status") Integer status, @Param("replyContent") String replyContent);

    // 更新反馈的状态为关闭（2）
    @Update("UPDATE Feedbacks SET status = 2 WHERE feedback_id = #{feedbackId}")
    int updateFeedbackStatus2(@Param("feedbackId") Integer feedbackId, @Param("status") int status);
}
